import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:zhrs/mainpage/test.dart';
import 'package:zhrs/mainpage/webviewpage.dart';
import 'package:zhrs/utils/StaticContent.dart' ;
import 'package:zhrs/utils/alert_utils.dart';
import 'package:zhrs/mainpage/mainBody.dart';
import 'package:zhrs/mainpage/exploreindex.dart';
import 'package:zhrs/mainpage/medicare.dart';
import 'package:zhrs/mainpage/user.dart';
import 'package:zhrs/utils/windowsSize.dart' ;

class Mainindex extends StatefulWidget
{
Mainindex({this.index=1,this.offset=1})
{

}
  static WebViewController controller;
  int index;
  int offset;

@override
MainindexState createState() => MainindexState();
}

class MainindexState extends State<Mainindex>
{
  Webviewpage webviewpage;
  bool appBar=false;
  BottomItem item_now;
  List<BottomItem> bottomItems;
  int _defaultIndex=0;
  List<BottomNavigationBarItem> barItems;
  List<bool> offset=[false,true,true,true];
  static List<bool> hide=[true,false];

   void changes({String url})
  {
if(Webviewpage.finishpageload)
  {
   // Webviewpage.finishpageload=false;

    if(!Webviewpage.istop)
      {
        if(!Webviewpage.Isinformation)
        {
          if(url!=null)
          {
            //Webviewpage.top=url;
            Webviewpage.controller.loadUrl(url);
          }

          setState(() {
            for (int i=0;i<hide.length;i++)
            {
              hide[i]=!hide[i];
            }
          });
        }
    else
    {
      if(hide[0]==false)
      {

        Webviewpage.controller.loadUrl(url);
      }
      else
      {
        if(url!=null)
        {
          Webviewpage.top=url;
          Webviewpage.controller.loadUrl(url);
        }
        setState(() {
          for (int i=0;i<hide.length;i++)
          {
            hide[i]=!hide[i];
          }
        });
      }
    }
      }


  }



  }
  @override
  void initState() {
    webviewpage=new Webviewpage(reset: this.changes,);

    for(int i=1;i<=4;i++)
      {
        if(i==widget.offset)
          {
            offset[widget.offset]=false;
          }
        else
          offset[widget.offset]=true;
      }
    StaticContent.contexts=context;
    StaticContent.pageState=this;
    // TODO: implement initState
    super.initState();


    bottomItems=[
      BottomItem(title: "首页",key:GlobalKey(debugLabel: "home"),icon: Icon(Icons.home),url: "https://www.dzsbk.cn/smartDemo/page/main/index.html",isOffset: false,widget: MainBody(index: widget.index, reset: changes,context: this.context,pageState: this,)),
      BottomItem(title: "发现",key:GlobalKey(debugLabel: "find"),icon: Icon(Icons.find_in_page),url: "https://www.dzsbk.cn/smartDemo/page/explore/index.html",widget: Exploreindex(index:  widget.index,reset: changes,)),
      BottomItem(title: "医疗",key:GlobalKey(debugLabel: "his"),icon: Icon(Icons.insert_invitation),url: "https://www.dzsbk.cn/smartDemo/page/medicare/index.html",widget: Medicare( reset: changes,)),
      BottomItem(title: "我的",key:GlobalKey(debugLabel: "my"),icon: Icon(Icons.person),url: "https://www.dzsbk.cn/smartDemo/page/user/index.html",widget: User(index:  widget.index,reset: changes,)),
      //BottomItem(title: "测试",key:GlobalKey(debugLabel: "test"),icon: Icon(Icons.person),url: "https://www.dzsbk.cn/smartDemo/page/user/index.html",widget: Test(index:  widget.index,)),
    ];

    item_now=bottomItems[0];
    barItems=bottomItems.map((item){
      return BottomNavigationBarItem(
        icon: item.icon,
        title: Text(item.title),
      );
    }).toList();
  }

  Onchange(int index,String url)
  {

    Webviewpage.Isinformation=true;
   this.changes(url:url, );
    //Navigator.push(context, MaterialPageRoute(builder:(context) =>Webviewpage(url:url ,Isinformation: true,)));
  }

  _onTapBottomItems(index)
  {
    if(_defaultIndex==index) return;
    setState(() {
      _defaultIndex=index;
      bottomItems.forEach((item) async{
        if(bottomItems[index]==item&&item.isOffset==true)  {
          item_now=item;
          item.isOffset=false;
        }
        else
          item.isOffset=true;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    windowsSize.mapViewWidth = MediaQuery
        .of(context)
        .size
        .width;
    windowsSize.mapViewHeight = MediaQuery
        .of(context)
        .size
        .height;
    if(  !StaticContent.isonce)
    {

      new Timer(new Duration(seconds: 1), () {
        // 只在倒计时结束时回调
        //alertDialog(context, "通知: "+StaticContent.information);
        this.Onchange(StaticContent.index, StaticContent.url);
        StaticContent.isonce=true;
      });
    }

    // TODO: implement build
    SafeArea safeArea=SafeArea(
      child: Scaffold(
        appBar: appBar?AppBar(leading:Icon(Icons.keyboard_arrow_left) ,):null,
        body: Stack(

          children: <Widget>[

            Offstage(
              offstage: hide[0],
              child: webviewpage,

          ),


          Offstage(
          offstage:  hide[1],
             child:   new Scaffold(
                body: Stack(
              children: bottomItems.map((item){
              return SafeArea(
                child: Offstage(
                  offstage: item.isOffset,
                  child://item.widget ,
                  WillPopScope(
                    // key:GlobalKey(debugLabel: item.title),
                      child:item.widget ,
                      onWillPop: () async{
                        // if(Platform.isAndroid)  SystemChannels.textInput.invokeMethod('TextInput.hide');
                        Future<bool> canlogout= onWillPop(context);
                        canlogout.then((value){
                          if(value==true) dispose();
                        });
                        return canlogout;
                      }),
                ),
              );
            }).toList(),
          ),
          bottomNavigationBar: BottomNavigationBar(
            items: barItems,
            onTap: _onTapBottomItems,
            currentIndex: _defaultIndex,
            type:BottomNavigationBarType.fixed ,
          ),
        ),
      )

          ],
        ),
      ),



    );
    return safeArea;
  }
}

class BottomItem{
  final String title;
  final Icon icon;
  bool isOffset;
  final String url;
  Key key;
  Widget widget;
  BottomItem({this.title,this.icon,this.url,this.isOffset=true,this.key,this.widget});
}